<?php
/**
 * Created by PhpStorm.
 * User: caoyixian
 * Date: 2017/6/2 0002
 * Time: 下午 12:03
 */



class Site_service_model extends Admin_Model{

     protected $table='site_service';

    public function __construct()
    {
        parent::__construct();
        $this->load->driver('cache',array('adapter' => 'file'));

    }

     public function add($data=array()){

         if(!empty($data)){
            $this->db->insert($this->table,$data);
            return $this->db->insert_id();
         }
             return false;


     }

    public function update($data=array()){

        if(!empty($data)){
            $result = $this->db->where(['id'=>$data['id']])->update($this->table,$data);
            return $result;
        }
        return false;

    }

    public function userLogin($username){
        $key = $this->config->item('remote_login_key');
        //$key = "26cde3d3616e3ab8118c0e318ee38ca1";
        $data['sign'] = md5($username.$key);
        $data['mobile'] = $username;
        $data['app_key'] = '121o8d6f83js749m';
        $info = $this->curl_get($data);
//        var_dump($info);
        if(!$info) {
            return false;
        }
            //检查是否存在此客服
            $user_info =  $info->info;
            $check =  $this->db->where(['site_id'=>$user_info->site_id,'username'=>$user_info->mobile])->limit(1)->get($this->table);
            if(!$check->row()){                //增加
                $this->db->insert($this->table,['site_id'=>$user_info->site_id,'username'=>$user_info->mobile,'nickname'=>$user_info->nickname]);
                $this->db->insert('site_service_relation',['site_id'=>$user_info->site_id,'mobile'=>$user_info->mobile,'token'=>$user_info->mobile]);
            }
            //var_dump($user_info);
            return $user_info;

        /***
        $info =  $this->db->where(['username'=>$username])->get($this->table);

        if($info->row()){
            return $info->row();
        }
        return false;
        ***/

    }


    public function curl_get($data){
        //$url = "http://api.sixishop.com/h5/kefu_login?".http_build_query($data);
        $url = $this->config->item('remote_login_url');
        $url.= '?'.http_build_query($data);
        $ch = curl_init();
        curl_setopt($ch , CURLOPT_URL , $url);
        curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); //定义请求类型，当然那个提交类型那一句就不需要了
        //curl_setopt($ch , CURLOPT_POST, 1);
        //curl_setopt($ch , CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
        curl_close($ch);
        return  json_decode($output);
    }

    public function getRow($id){

       $info =  $this->db->where(['id'=>$id])->get($this->table);
       if($info->row()){
           return $info->row();
       }
       return [];
    }


     public function getAll(){
         $where='';
         $params = array();

         $db = $this->db;

         $title = $this->input->get('title');
         if(!empty($title)){
             if($where){
                 $where.= ' and ';
             }
             $where.= " title='".$title."' ";
             $params['title'] = $title;
         }

         if(!$where) {
             $total = $db->from($this->table)->count_all_results();
             $page = $this->getPageinate(base_url('admin/notes/notes_list'), $total, 15, 4,$params);
             $limit = $page['limit'];
             $offset = $page['offset'];
             $fetch = $db->from($this->table)
                 ->select('notes.*,notes_type.name as typename')
                 ->join('notes_type','notes_type.id=notes.type_id','left')
                 ->limit($limit)
                 ->offset($offset)
                 ->get();
             $list = $fetch->result();
         }else{
             $total = $db->from($this->table)->where($where)->count_all_results();
             $page = $this->getPageinate(base_url('admin/notes/notes_list'), $total, 15, 4,$params);
             $limit = $page['limit'];
             $offset = $page['offset'];
             $fetch = $db->from($this->table)
                 ->select('notes.*,notes_type.name as typename')
                 ->join('notes_type','notes_type.id=notes.type_id','left')
                 ->where($where)
                 ->limit($limit)
                 ->offset($offset)
                 ->get();
             $list = $fetch->result();
         }

         return ['page'=>$page['page'],'list'=>$list,'params'=>$params];
     }












}